<script setup>
import { ref, onMounted } from 'vue'
import {articleAPI} from "@/apis/index.js";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
import {useBoardStore} from "@/stores/index.js";

const router = useRouter()
const boardStore = useBoardStore()

// 数据
const sections = ref()
const loading = ref(false)

// 获取主页文章列表
const fetchHomeArticleList = async () => {
  try {
    loading.value = true
    const response = await articleAPI.getHomeArticleList()

    if (response.status === 'success') {
      sections.value = response.data.list.map(board => ({
        pBoardId: board.pBoardId,
        pBoardName: board.pBoardName,
        posts: board.articles.map(article => ({
          articleId: article.articleId,
          title: article.title
        }))
      }))
    }
  } catch (error) {
    ElMessage.error('Failed to retrieve the list of articles')
  } finally {
    loading.value = false
  }
}

// 点击帖子
const handlePostClick = (post) => {
  router.push(`/post/${post.articleId}`)
}

// 点击板块
const handleBoardClick = (pBoardId) => {
  // 更新全局板块状态
  boardStore.setActiveBoard(pBoardId, null)
}

onMounted( () => {
  fetchHomeArticleList()
})
</script>

<template>
  <div class="home-container">
    <div class="home-header">
      <div class="logo">
        <img src="@/assets/logo.png" alt="logo">
      </div>
      <p>Share activities, exchange experiences, and jointly build a beautiful campus life</p>
    </div>

    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="5" animated/>
    </div>
    
    <div v-else class="sections-grid">
      <div 
        v-for="section in sections"
        :key="section.pBoardId"
        class="section-card"
      >
        <router-link :to="`/board/${section.pBoardId}`" class="section-header" @click="handleBoardClick(section.pBoardId)">
          <h2 class="section-title">{{ section.pBoardName }}</h2>
        </router-link>
        
        <div class="posts-list">
          <div 
            v-for="post in section.posts" 
            :key="post.id"
            class="post-item"
            @click="handlePostClick(post)"
          >
            <span class="post-title">{{ post.title }}</span>
            <span class="post-arrow">→</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.home-container {
  padding: 20px;
  max-width: 1300px;
  margin: 0 auto;
  background-color: #f8f9fa;
  min-height: 100vh;

  .logo {
    img {
      height: 120px;
    }
  }
}

.home-header {
  text-align: center;
  margin-bottom: 40px;
  
  h1 {
    font-size: 2.5rem;
    color: #2c3e50;
    margin-bottom: 10px;
    font-weight: 600;
  }
  
  p {
    font-size: 1.1rem;
    color: #7f8c8d;
    margin: 0;
  }
}

.sections-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  
  @media (max-width: 768px) {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}

.section-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  }
}

.section-header {
  text-decoration: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #e9ecef;
}

.section-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
}

.view-more-btn {
  background: #3498db;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background-color 0.2s ease;
  
  &:hover {
    background: #2980b9;
  }
}

.posts-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.post-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: #f8f9fa;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  
  &:hover {
    background: #e9ecef;
    transform: translateX(4px);
  }
}

.post-title {
  font-size: 0.95rem;
  color: #495057;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-right: 12px;
}

.post-arrow {
  color: #6c757d;
  font-weight: bold;
  transition: transform 0.2s ease;
}

.post-item:hover .post-arrow {
  transform: translateX(4px);
  color: #3498db;
}

// 响应式设计
@media (max-width: 480px) {
  .home-container {
    padding: 16px;
  }
  
  .home-header h1 {
    font-size: 2rem;
  }
  
  .section-card {
    padding: 20px;
  }
  
  .section-title {
    font-size: 1.3rem;
  }
  
  .post-title {
    font-size: 0.9rem;
  }
}
</style>